package TCPClient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

import messangerTypes.*;


public class TCPSendWorker extends Thread
{
	//TODO: eventually make object output stream
	private OutputStream out;
	//user
	private User user;
	public boolean quit = false;
	

	public TCPSendWorker(User user, OutputStream out)
	{
		this.user = user;
		this.out = out;
	}
		
	@Override
	public void run()
	{
		String quitString = "quit";
		BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));
		//DataOutputStream outToServer = new DataOutputStream(this.out);
		ObjectOutputStream outToServer;
		try
		{
			outToServer = new ObjectOutputStream(this.out);
		} catch (IOException e1)
		{
			//exit
			return;
		}
		
		do{
			String message;
			System.out.println(">Enter a message: ");
			try
			{
				message = inFromUser.readLine();
				//outToServer.writeBytes(message + "\n");
				
				quit = quitString.contentEquals(message);
				
				if(!quit)
				{
					UserMessage um = new UserMessage(user, user, message);
					outToServer.writeObject(um);
				}
				
				Thread.sleep(500);
			}
			catch (Exception e)
			{
				quit = true;
			}			
		}while(!quit);
				
		try
		{
			inFromUser.close();
			outToServer.close();
		} 
		catch (IOException e)
		{
			//just move on. 
		}
	}

}
